package com.dingding.order.facade;

import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.RequestEntity;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.cancel.biz.ICancelOrderService;
import com.dingding.order.core.history.service.IHistoryOrderService;
import com.dingding.order.core.march.biz.IMarchOrderService;
import com.dingding.order.core.myorder.biz.IGetMyOrderService;
import com.dingding.order.core.myorder.dao.IGetMyOrderDao;
import com.dingding.order.core.pay.biz.IOrderPayService;
import com.dingding.order.core.special.biz.ISpecialOrderService;
import com.dingding.order.dto.cancel.special.ReqCancelSpecialOrderDTO;
import com.dingding.order.dto.cancel.special.ResCancelSpecialOrderDTO;
import com.dingding.order.dto.myorder.SpeCusOrdInfoResult;
import com.dingding.order.service.special.ISpecialCarOrderFacade;

/**
 * @Description:专车订单接口
 * @author fengshuonan
 * @date 2016年8月18日 下午2:30:10
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("specialCarOrderFacade")
public class SpecialCarOrderFacade implements ISpecialCarOrderFacade{

	@Resource(name = "specialOrderService")
	private ISpecialOrderService specialOrderService;
	
	@Resource(name = "customerUnderWayOrderService")
	private IMarchOrderService customerUnderWayOrderService;
	
	@Resource(name = "driverUnderWayOrderService")
	private IMarchOrderService driverUnderWayOrderService;
	
	@Resource(name = "specialOrderPayService")
	private IOrderPayService orderPayService;
	
	@Resource(name = "customerOrderService")
	private IHistoryOrderService customerOrderService;
	
	@Resource(name = "driverOrderService")
	private IHistoryOrderService driverOrderService;
	
	@Resource(name = "driverMergeOrderService")
	private IHistoryOrderService driverMergeOrderService;
	
	@Resource(name = "getMyOrderService")
	private IGetMyOrderService getMyOrderService;
	
	@Resource(name = "getMyOrderDao")
	private IGetMyOrderDao getMyOrderDao;
	
	@Resource(name = "specialCancelOrderService")
	private ICancelOrderService specialCancelOrderService;
	
	
	/**
	 * @Description: 专车实时定位，获取专车最快到达时间和周围车辆
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String boardingTime(String clientData){
		return this.specialOrderService.boardingTime(clientData);
	}
	
	/**
	 * @Description:获取乘客的所有进行中订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String getCustomerUnderWayOrders(String clientData){
		return this.customerUnderWayOrderService.getMarchOrder(clientData);
	}
	
	/**
	 * @Description:获取司机的所有进行中订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String getDriverUnderWayOrders(String clientData){
		return this.driverUnderWayOrderService.getMarchOrder(clientData);
	}
	
	/**
	 * @Description:订单支付
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String orderPay(String clientData){
		return this.orderPayService.orderPay(clientData);
	}

	/**
	 * @Description:专车订单id
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public boolean orderPayNotify(String special){
		return this.specialOrderService.orderPayNotify(special);
	}

	/**
	 * @Description:获取乘客的历史订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String getCustomerHistoryOrders(String clientData) {
		return this.customerOrderService.getHistoryOrders(clientData);
	}

	/**
	 * @Description:获取司机的历史订单
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public String getDriverHistoryOrders(String clientData) {
		return this.driverOrderService.getHistoryOrders(clientData);
	}

	/**
	 * 
	 * @Title: completeAndCancelOrder 
	 * @Description: 获取已完成和已取消订单
	 * @param clientData 客户端请求参数
	 * @author YHQ 
	 * @since V2.5.5
	 * @return String    返回类型
	 * 
	 */
	@Override
	public String completeAndCancelOrder(String clientData) {
		return this.driverMergeOrderService.getHistoryOrders(clientData);
	}
	
	
	/**
	 * @Description:获取某个用户的专车订单信息
	 * @param userId 用户id
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public List<SpeCusOrdInfoResult> getSpecialCustomerOrderInfo(String userId){
		
		return this.getMyOrderService.getCusSpecialOrder(userId);
	}
	
	
	/**
	 * @Description:获取乘客的专车订单信息通过手机号
	 * @param tellphone 电话号码
	 * @author fengshuonan
	 * @return List<SpeCusOrdInfoResult>
	 * @since 2.4
	 */
	@Override
	public List<SpeCusOrdInfoResult> getSpecialCustomerOrderInfoByPhone(String tellphone){
		
		String userId = this.getMyOrderDao.getUserIdByPhone(tellphone);
		
		if(ToolUtil.isBlank(userId)){
			return null;
		}
		
		return this.getMyOrderService.getCusSpecialOrder(userId);
	}

	/**
	 * @Description:取消专车订单
	 * @param orderId 订单id
	 * @author fengshuonan
	 * @return boolean true 成功  false 失败
	 * @since 2.4
	 */
	@Override
	public boolean cancelSpecialOrder(String orderId) {
		
		RequestEntity<ReqCancelSpecialOrderDTO> requestEntity = new RequestEntity<ReqCancelSpecialOrderDTO>();
		
		ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO = new ReqCancelSpecialOrderDTO();
		reqCancelSpecialOrderDTO.setCancelType(1);// 取消类型  1:乘客取消 2:车主取消 3:自动取消
		reqCancelSpecialOrderDTO.setOid(orderId);
		
		requestEntity.setRequestBody(reqCancelSpecialOrderDTO);
		
		String result = this.specialCancelOrderService.cancelOrder(JSONUtil.toJSONString(requestEntity));
		
		ResponseEntity<ResCancelSpecialOrderDTO> response = (ResponseEntity<ResCancelSpecialOrderDTO>) JSONUtil.parseObject(result, ResponseEntity.class);
		
		if(response.getCode() == HttpStatus.SC_OK){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * @Description:获取乘客的专车订单（未完成的）通过用户id
	 * @author fengshuonan
	 * @since 2.4
	 */
	@Override
	public List<SpeCusOrdInfoResult> getCusSpeOrderByUserId(String userId) {
		
		return this.getMyOrderService.getCusSpecialOrder(userId);
	}
}
